package org.swift.mashup.engine.concurrency.execution;

import java.util.BitSet;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;




public class ConditionPattern {
	private int id;
	private Map<String, Condition> conditions = new HashMap<String, Condition>();
	private Set<Rule> rules = new HashSet<Rule>();
	private BitSet mask = new BitSet();
	private RulePattern parent = null;
	
	public ConditionPattern(){
	}
	public ConditionPattern(int id,RulePattern parent){
		this.setId(id);
		this.setMask(new BitSet());
		this.getMask().set(id);
		this.parent = parent;
	}
	public ConditionPattern(int id, BitSet mask,RulePattern parent){
		this.setId(id);
		this.setMask(mask);
		this.parent = parent;
	}
	public ConditionPattern(int id, Condition condition,RulePattern parent){
		this.setId(id);
		this.setMask(condition.getMask());
		this.getConditions().put(condition.getExpression(), condition);
		this.parent = parent;
	}
	public ConditionPattern(int id, Collection<? extends Condition> conditions,RulePattern parent){
		this.setId(id);
		this.setMask(computeConditionMask(conditions));
		for (Condition condition:conditions){
			this.conditions.put(condition.getExpression(), condition);
		}
		this.parent = parent;
	}
	public static BitSet computeConditionMask(Collection<? extends Condition> conditions){
		BitSet mask = new BitSet();
		for (Condition condition:conditions) {
			mask.or(condition.getMask());
		}
		return mask;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Map<String, Condition> getConditions() {
		return conditions;
	}
	public void setConditions(Map<String, ? extends Condition> conditions) {
		this.conditions.clear();
		this.conditions.putAll(conditions);
	}
	public Set<Rule> getRules() {
		return rules;
	}
	public void setRules(Set<? extends Rule> rules) {
		this.rules.clear();
		this.rules.addAll(rules);
	}
	public BitSet getMask() {
		return mask;
	}
	public void setMask(BitSet mask) {
		this.mask = mask;
	}
	public RulePattern getParent() {
		return parent;
	}
	
}
